package com.smartwasser.yunhtgl.apphtgl.presenter;

import android.os.Handler;

import com.smartwasser.yunhtgl.apphtgl.bean.CaseProcess1Bean;
import com.smartwasser.yunhtgl.apphtgl.bean.CompanyBean;
import com.smartwasser.yunhtgl.apphtgl.network.RetrofitApi;
import com.smartwasser.yunhtgl.apphtgl.network.Retrofits;
import com.smartwasser.yunhtgl.apphtgl.view.CaseProcess1View;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CaseProcess1PresenterImpl implements CaseProcess1Presenter {
    private CaseProcess1View caseProcess1View;
    private Handler mHandler = new Handler();

    public CaseProcess1PresenterImpl(CaseProcess1View caseProcess1View) {
        this.caseProcess1View = caseProcess1View;
    }

    /**
     * 待完成合同备案
     *
     * @param JSESSIONID
     * @param year
     * @param season
     * @param roleCode
     */
    @Override
    public void getMessages(final String JSESSIONID, String year, String season, String roleCode) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getCaseProcess1(JSESSIONID, year, season, roleCode).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseProcess1Bean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                caseProcess1View.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(final CaseProcess1Bean caseProcessBean) {
                caseProcess1View.success(caseProcessBean);

            }
        });
    }

    @Override
    public void getCompany(final String JSESSIONID) {
        Retrofits.getInstance().createReq(RetrofitApi.class).getCompany(JSESSIONID).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CompanyBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                caseProcess1View.error();
                Logger.e("onError");
            }

            @Override
            public void onNext(CompanyBean companyBean) {
                caseProcess1View.successCompany(companyBean);
                Logger.e("onNext");
            }
        });
    }

    @Override
    public void shMessage(String JSESSIONID, String ids, String flagF, String comment) {
        Retrofits.getInstance().createReq(RetrofitApi.class).shMessage(JSESSIONID, ids, flagF, comment).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Object>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                caseProcess1View.errorSH();
                Logger.e("onError");
            }

            @Override
            public void onNext(Object object) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        caseProcess1View.successSH();
                    }
                }, 1000);
            }
        });
    }
}